﻿@model List<OnlineEducation.Data.Entities.TeachingPlan>
@{
    ViewData["Title"] = "教学计划";

}
@section Styles{


    <style>
        @@media (min-width: 576px) {
            .modal-dialog {
                max-width: 50% !important;
            }
        }
    </style>
}

@functions {

    public string showStatus(int status)
    {
        if (status == 0)
        {
            return "未开始";
        }
        else if (status == 1)
        {
            return "进行中";
        }
        else  
        {
            return "完成";
        }
        
    }
}
<!-- Start Content-- >
<div class="container-fluid" >
<!-- start page title -->
<div class="row">
    <div class="col-12">
        <div class="page-title-box">
            <div class="page-title-right">

                <ol class="breadcrumb m-0">
                    <li class="breadcrumb-item"> <a href="javascript: void(0);"> <i class="fa fa-map-marker-alt"> </i> </a> </li>
                    <li class="breadcrumb-item"> <a href="javascript: void(0);"> 教学计划</a> </li>
                    <li class="breadcrumb-item active"> 查询</li>
                </ol>
            </div>
            <h4 class="page-title"> 教学计划</h4>
        </div>
    </div>
</div>
<!-- end page title -->
<div class="row hsm">
    <div class="col-md-6 col-xl-3">
        <div class="widget-rounded-circle card-box">
            <div class="row">
                <div class="col-6">
                    <div class="avatar-lg rounded-circle bg-primary">
                        <i class="mdi mdi-text-subject font-22 avatar-title text-white"></i>
                    </div>
                </div>
                <div class="col-6">
                    <div class="text-right">
                        <h3 class="text-dark mt-1"><span data-plugin="counterup"></span></h3>
                        <p class="text-muted mb-1 text-truncate"></p>
                    </div>
                </div>
            </div>
        </div>
    </div>



    <div class="col-md-6 col-xl-3">
        <div class="widget-rounded-circle card-box">
            <div class="row">
                <div class="col-6">
                    <div class="avatar-lg rounded-circle bg-success">
                        <i class="fe-check-circle font-22 avatar-title text-white"></i>
                    </div>
                </div>
                <div class="col-6">
                    <div class="text-right">
                        <h3 class="text-dark mt-1"><span data-plugin="counterup"></span></h3>
                        <p class="text-muted mb-1 text-truncate"></p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-md-6 col-xl-3">
        <div class="widget-rounded-circle card-box">
            <div class="row">
                <div class="col-6">
                    <div class="avatar-lg rounded-circle bg-danger">
                        <i class="mdi mdi-playlist-remove font-22 avatar-title text-white"></i>
                    </div>
                </div>
                <div class="col-6">
                    <div class="text-right">
                        <h3 class="text-dark mt-1"><span data-plugin="counterup"></span></h3>
                        <p class="text-muted mb-1 text-truncate"></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-6 col-xl-3">
        <div class="widget-rounded-circle card-box">
            <div class="row">
                <div class="col-6">
                    <div class="avatar-lg rounded-circle bg-danger">
                        <i class="mdi mdi-playlist-remove font-22 avatar-title text-white"></i>
                    </div>
                </div>
                <div class="col-6">
                    <div class="text-right">
                        <h3 class="text-dark mt-1"><span data-plugin="counterup"></span></h3>
                        <p class="text-muted mb-1 text-truncate"></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-12">
        <div class="card-box">

            <button onclick="javascript: window.location.href='/TeachingPlan/Add/'" type="button" class="btn btn-primary  waves-effect waves-light float-right ladda-button" data-style="expand-left">
                添加教学计划
            </button>
            <h4 class="header-title mb-4">教学计划列表</h4>

            <div class="table-responsive" data-pattern="priority-columns" data-add-focus-btn="false" data-add-display-all-btn="false" data-add-display-btn="false">
                <table class="table table-small-font  table-striped" id="datatable" style="width:100%">
                    <thead>
                        <tr>
                            <th width="10%" data-priority="1">计划序号</th>
                            <th width="10%" data-priority="1">课程名称</th>
                            <th width="10%" data-priority="2">课时数</th>
                            <th width="10%" data-priority="1">招生数量</th>
                            <th width="10%" data-priority="2">开始时间</th>
                            <th width="10%" data-priority="2">结束时间</th>
                      
                            <th width="10%" data-priority="2">创建时间</th>
                            <th width="8%" data-priority="2">状态</th>
                            <th width="8%" data-priority="1">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var plan in Model)
                        {

                            DateTime? finalEndTime = null;
                            DateTime finalStartTime;


                            var lastPlanItem = plan.TeachingPlanItems.OrderByDescending(n => n.BeginTime).FirstOrDefault();
                            var firstPlanItem = plan.TeachingPlanItems.OrderBy(n => n.BeginTime).FirstOrDefault();

                            if (lastPlanItem != null)
                            {
                                var courseItem = plan.Course.Lessons.Where(n => n.Id == lastPlanItem.LessonId).FirstOrDefault();

                                if (courseItem != null)
                                {
                                    finalEndTime = lastPlanItem.BeginTime.AddMinutes(courseItem.Duration);
                                }
                            }
                            if (firstPlanItem != null)
                            {
                                finalStartTime = firstPlanItem.BeginTime;
                            }
                            else
                            {
                                finalStartTime = plan.BeginTime;
                            }
                            <tr>
                                <td>#@plan.Id.ToString().PadLeft(4, '0') </td>
                                <td>@plan.Course.Title-@plan.Course.SubTitle </td>
                                <td>@plan.TeachingPlanItems.Count() </td>
                                <td>@plan.StudentPlanCount </td>

                                <td>@finalStartTime </td>

                                @if (finalEndTime.HasValue)
                                {
                                    <td>@finalEndTime.Value</td>
                                }
                                else
                                {
                                    <td></td>
                                }


                          
                                <td>@plan.CreatedTime </td>
                                <td>@(showStatus(plan.Status)) </td>
                                <td>
                                    <div class='btn-group dropdown'>

                                        <a href='javascript: void(0);' class='table-action-btn dropdown-toggle arrow-none btn btn-outline-secondary waves-effect btn-sm' data-toggle='dropdown' aria-expanded='false'> <i class='ti-menu'> </i> </a>

                                        <div class='dropdown-menu dropdown-menu-right'>

                                            <a class='dropdown-item' href='/TeachingPlan/Edit/@plan.Id'> <i class='mdi mdi-pencil mr-2 text-muted font-18 vertical-middle'> </i> 修改</a>
                                           
                                            <a class='dropdown-item' target="_blank" href='/TeachingPlan/Preview/@plan.Id'>
                                                <i class='mdi mdi-check mr-2 text-muted font-18 vertical-middle'></i>预览
                                            </a>
                                            <a class='dropdown-item' href='#' onclick='Remove("@plan.Id")'> <i class='mdi mdi-delete-forever mr-2 text-muted font-18 vertical-middle'> </i> 删除</a>
                                        </div>
                                    </div>





                                </td>
                            </tr>
                        }

                    </tbody>
                </table>
            </div>

        </div><!-- end col -->
    </div>
    <!-- end row -->
</div>
<div id="modalDefault" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">

        </div>
    </div>
</div><!-- /.modal -->
@section Scripts{
    @await Html.PartialAsync("_ValidationScriptsPartial")
    <script src="~/assets/libs/moment/moment.min.js"></script>

    <script>


        var table;

        $(function () {

            table = $('#datatable').DataTable({
                "oLanguage": {
                    "sLengthMenu": "每页显示 _MENU_ 条记录",
                    "sZeroRecords": "对不起，没有匹配的数据",
                    "sInfo": "第 _START_ - _END_ 条 / 共 _TOTAL_ 条数据",
                    "sInfoEmpty": "没有匹配的数据",
                    "sInfoFiltered": "(数据表中共 _MAX_ 条记录)",
                    "sProcessing": "正在加载中...",
                    "sSearch": "全文搜索：",
                    "oPaginate": {
                        "sFirst": "第一页",
                        "sPrevious": " 上一页 ",
                        "sNext": " 下一页 ",
                        "sLast": " 最后一页 "
                    }
                },
                language: {
                    paginate:
                        { previous: "<i class='mdi mdi-chevron-left'>", next: "<i class='mdi mdi-chevron-right'>" }
                },
                drawCallback: function () { $(".dataTables_paginate > .pagination").addClass("pagination-rounded"); $('.table-responsive').responsiveTable('update'); },

                "responsive": true,
                "lengthChange": false,
                "searching": true,
                "order": [[8, 'desc']],
                "aoColumnDefs": [{ "bSortable": false, "aTargets": [1, 2, 3, 4, 5, 6, 7,0] }],
                "serverSide": false,

                //ajax: {
                //    url: '/Student/GetAll',
                //    type: 'POST',
                //    async: true,
                //    //headers: {
                //    //    "XSRF-TOKEN": document.querySelector('[name="__RequestVerificationToken"]').value
                //    //},
                //    data: function (data) {

                //        return {
                //            param: data
                //        };
                //    }
                //},

                //columns: [
                //    {
                //        title: "用户名",
                //        data: "UserName",
                //        name: "UserName",

                //    },
                //    {
                //        title: "邮箱",
                //        data: "Email",
                //        name: "Email"
                //    },
                //    {
                //        title: "手机",
                //        data: "PhoneNumber",
                //        name: "PhoneNumber"
                //    },
                //    {
                //        title: "状态",
                //        data: "Status",
                //        name: "Status",
                //        render: function (data, type, row) {

                //            if (data == 1) {
                //                return "<span class='badge badge-success'>正常</span>"
                //            }
                //            else {
                //                return "<span class='badge badge-dark'>锁定</span>"
                //            }
                //        }
                //    },
                //    {
                //        title: "建立日期",
                //        data: "CreatedTime",
                //        name: "CreatedTime",
                //        render: function (data, type, row) {
                //            return window.moment(data).format("YYYY-MM-DD HH:mm:ss");
                //        }
                //    },
                //    {
                //        title: "",
                //        data: null,
                //        name: null,
                //        render: function (data, type, row) {

                //            var html = "<div class='btn-group dropdown'>" +
                //                "<a href='javascript: void(0);' class='table-action-btn dropdown-toggle arrow-none btn btn-outline-secondary waves-effect btn-sm' data-toggle='dropdown' aria-expanded='false'><i class='ti-menu'></i></a>" +
                //                "<div class='dropdown-menu dropdown-menu-left'>" +
                //                "<a class='dropdown-item' href='#' onclick='ShowPopup(\"/Subject/AddEdit/" + row["ID"] + "\")'><i class='mdi mdi-pencil mr-2 text-muted font-18 vertical-middle'></i>修改</a>";

                //            if (row["Status"] == 1) {
                //                html = html + "<a class='dropdown-item' href='#' onclick='UpdateStatus(\"" + row["ID"] + "\")'><i class='mdi mdi-playlist-remove mr-2 text-muted font-18 vertical-middle'></i>锁定</a>";
                //            } else {
                //                html = html + "<a class='dropdown-item' href='#' onclick='UpdateStatus(\"" + row["ID"] + "\")'><i class='mdi mdi-check mr-2 text-muted font-18 vertical-middle'></i>解除锁定/a>";
                //            }
                //            html = html + "<a class='dropdown-item' href='#' onclick='UpdateStatus(\"" + row["ID"] + "\")'><i class='mdi mdi-check mr-2 text-muted font-18 vertical-middle'></i>修改密码/a>";
                //            html = html + "<a class='dropdown-item' href='#' onclick='Remove(\"" + row["ID"] + "\")'><i class='mdi mdi-delete-forever mr-2 text-muted font-18 vertical-middle'></i>删除</a></div></div>";

                //            return html;

                //        }
                //    }

                //]


            });
        });


        function ShowPopup(url) {
            var modalId = 'modalDefault';
            var modalPlaceholder = $('#' + modalId + ' .modal-dialog .modal-content');
            $.get(url)
                .done(function (response) {
                    modalPlaceholder.html(response);
                    popup = $('#' + modalId + '').modal({
                        keyboard: false,
                        backdrop: 'static'
                    });
                });
        }


        function SubmitForm(form, url) {
            $(form).removeData('validator');
            $(form).removeData('unobtrusiveValidation');
            $.validator.unobtrusive.parse(form);
            if ($(form).valid()) {
                var data = $(form).serializeJSON();
                data = JSON.stringify(data);

                $.ajax({
                    type: 'POST',
                    url: url,
                    data: data,
                    contentType: 'application/json',
                    success: function (data) {
                        if (data.success) {
                            popup.modal('hide');
                            ShowMessage(data.message);
                            setTimeout(function () {
                                window.location.href = "/TeachingPlan/Index";
                            }, 2000);

                        } else {
                            ShowMessageError(data.message);
                        }
                    }
                });

            }
            return false;
        }

        function Remove(id) {


            Swal.fire({
                title: "删除",
                text: "确定删除?",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#dd4b39",
                confirmButtonText: "是的，删除!",
                cancelButtonText: "取消",
                icon: "warning",
                buttons: true,
                dangerMode: true,

            }).then(function (willdelete) {
                console.log(willdelete);
                if (!willdelete.value) return;
                $.ajax({
                    type: 'POST',
                    url: '/TeachingPlan/Delete/' + id,
                    success: function (data) {
                        if (data.success) {
                            ShowMessage(data.message);
                            setTimeout(function () {
                                window.location.href = "/TeachingPlan/Index";
                            }, 2000);
                        } else {
                            ShowMessageError(data.message);
                        }
                    }
                });
            });


        }

        //function UpdateStatus(id) {

        //    $.ajax({
        //        type: 'POST',
        //        url: '/TeachingPlan/ChangeStatus/' + id,
        //        success: function (data) {
        //            if (data.success) {
        //                ShowMessage(data.message);
        //                setTimeout(function () {
        //                    window.location.href = "/TeachingPlan/Index";
        //                }, 2000);
        //            } else {
        //                ShowMessageError(data.message);
        //            }
        //        }
        //    });



        //}
    </script>
}
